home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / PNGLIB06.ZIP / PNGWUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  30.2 KB  |  1,063 lines

  1.  
  2. /* pngwutil.c - utilities to write a png file
  3.  
  4.    pnglib version 0.6
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  7.    May 1, 1995
  8.    */
  9. #define PNG_INTERNAL
  10. #include "png.h"
  11.  
  12. /* place a 32 bit number into a buffer in png byte order.  We work
  13.    with unsigned numbers for convenience, you may have to cast
  14.    signed numbers (if you use any, most png data is unsigned). */
  15. void
  16. png_save_uint_32(png_byte *buf, png_uint_32 i)
  17. {
  18.    buf[0] = (png_byte)((i >> 24) & 0xff);
  19.    buf[1] = (png_byte)((i >> 16) & 0xff);
  20.    buf[2] = (png_byte)((i >> 8) & 0xff);
  21.    buf[3] = (png_byte)(i & 0xff);
  22. }
  23.  
  24. /* place a 16 bit number into a buffer in png byte order */
  25. void
  26. png_save_uint_16(png_byte *buf, png_uint_16 i)
  27. {
  28.    buf[0] = (png_byte)((i >> 8) & 0xff);
  29.    buf[1] = (png_byte)(i & 0xff);
  30. }
  31.  
  32. /* write a 32 bit number */
  33. void
  34. png_write_uint_32(png_struct *png_ptr, png_uint_32 i)
  35. {
  36.    png_byte buf[4];
  37.  
  38.    buf[0] = (png_byte)((i >> 24) & 0xff);
  39.    buf[1] = (png_byte)((i >> 16) & 0xff);
  40.    buf[2] = (png_byte)((i >> 8) & 0xff);
  41.    buf[3] = (png_byte)(i & 0xff);
  42.    png_write_data(png_ptr, buf, 4);
  43. }
  44.  
  45. /* write a 16 bit number */
  46. void
  47. png_write_uint_16(png_struct *png_ptr, png_uint_16 i)
  48. {
  49.    png_byte buf[2];
  50.  
  51.    buf[0] = (png_byte)((i >> 8) & 0xff);
  52.    buf[1] = (png_byte)(i & 0xff);
  53.    png_write_data(png_ptr, buf, 2);
  54. }
  55.  
  56. /* Write a png chunk all at once.  The type is an array of ASCII characters
  57.    representing the chunk name.  The array must be at least 4 bytes in
  58.    length, and does not need to be null terminated.  To be safe, pass the
  59.    pre-defined chunk names here, and if you need a new one, define it
  60.    where the others are defined.  The length is the length of the data.
  61.    All the data must be present.  If that is not possible, use the
  62.    png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
  63.    functions instead.  */
  64. void
  65. png_write_chunk(png_struct *png_ptr, png_byte *type,
  66.    png_byte *data, png_uint_32 length)
  67. {
  68.    /* write length */
  69.    png_write_uint_32(png_ptr, length);
  70.    /* write chunk name */
  71.    png_write_data(png_ptr, type, (png_uint_32)4);
  72.    /* reset the crc and run the chunk name over it */
  73.    png_reset_crc(png_ptr);
  74.    png_calculate_crc(png_ptr, type, (png_uint_32)4);
  75.    /* write the data and update the crc */
  76.    if (length)
  77.    {
  78.       png_calculate_crc(png_ptr, data, length);
  79.       png_write_data(png_ptr, data, length);
  80.    }
  81.    /* write the crc */
  82.    png_write_uint_32(png_ptr, ~png_ptr->crc);
  83. }
  84.  
  85. /* Write the start of a png chunk.  The type is the chunk type.
  86.    The total_length is the sum of the lengths of all the data you will be
  87.    passing in png_write_chunk_data() */
  88. void
  89. png_write_chunk_start(png_struct *png_ptr, png_byte *type,
  90.    png_uint_32 total_length)
  91. {
  92.    /* write the length */
  93.    png_write_uint_32(png_ptr, total_length);
  94.    /* write the chunk name */
  95.    png_write_data(png_ptr, type, (png_uint_32)4);
  96.    /* reset the crc and run it over the chunk name */
  97.    png_reset_crc(png_ptr);
  98.    png_calculate_crc(png_ptr, type, (png_uint_32)4);
  99. }
  100.  
  101. /* write the data of a png chunk started with png_write_chunk_start().
  102.    Note that multiple calls to this function are allowed, and that the
  103.    sum of the lengths from these calls *must* add up to the total_length
  104.    given to png_write_chunk_start() */
  105. void
  106. png_write_chunk_data(png_struct *png_ptr, png_byte *data, png_uint_32 length)
  107. {
  108.    /* write the data, and run the crc over it */
  109.    if (length)
  110.    {
  111.       png_calculate_crc(png_ptr, data, length);
  112.       png_write_data(png_ptr, data, length);
  113.    }
  114. }
  115.  
  116. /* finish a chunk started with png_write_chunk_start() */
  117. void
  118. png_write_chunk_end(png_struct *png_ptr)
  119. {
  120.    /* write the crc */
  121.    png_write_uint_32(png_ptr, ~png_ptr->crc);
  122. }
  123.  
  124. /* simple function to write the signature */
  125. void
  126. png_write_sig(png_struct *png_ptr)
  127. {
  128.    /* write the 8 byte signature */
  129.    png_write_data(png_ptr, png_sig, (png_uint_32)8);
  130. }
  131.  
  132. /* Write the IHDR chunk, and update the png_struct with the necessary
  133.    information.  Note that the rest of this code depends upon this
  134.    information being correct.  */
  135. void
  136. png_write_IHDR(png_struct *png_ptr, png_uint_32 width, png_uint_32 height,
  137.    int bit_depth, int color_type, int compression_type, int filter_type,
  138.    int interlace_type)
  139. {
  140.    png_byte buf[13]; /* buffer to store the IHDR info */
  141.  
  142.    /* pack the header information into the buffer */
  143.    png_save_uint_32(buf, width);
  144.    png_save_uint_32(buf + 4, height);
  145.    buf[8] = bit_depth;
  146.    buf[9] = color_type;
  147.    buf[10] = compression_type;
  148.    buf[11] = filter_type;
  149.    buf[12] = interlace_type;
  150.    /* save off the relevent information */
  151.    png_ptr->bit_depth = bit_depth;
  152.    png_ptr->color_type = color_type;
  153.    png_ptr->interlaced = interlace_type;
  154.    png_ptr->width = width;
  155.    png_ptr->height = height;
  156.  
  157.    switch (color_type)
  158.    {
  159.       case 0:
  160.       case 3:
  161.          png_ptr->channels = 1;
  162.          break;
  163.       case 2:
  164.          png_ptr->channels = 3;
  165.          break;
  166.       case 4:
  167.          png_ptr->channels = 2;
  168.          break;
  169.       case 6:
  170.          png_ptr->channels = 4;
  171.          break;
  172.    }
  173.    png_ptr->pixel_depth = bit_depth * png_ptr->channels;
  174.    png_ptr->rowbytes = ((width * (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  175.    /* set the usr info, so any transformations can modify it */
  176.    png_ptr->usr_width = png_ptr->width;
  177.    png_ptr->usr_bit_depth = png_ptr->bit_depth;
  178.     png_ptr->usr_channels = png_ptr->channels;
  179.  
  180.    /* write the chunk */
  181.    png_write_chunk(png_ptr, png_IHDR, buf, (png_uint_32)13);
  182. }
  183.  
  184. /* write the palette.  We are careful not to trust png_color to be in the
  185.    correct order for PNG, so people can redefine it to any convient
  186.    structure. */
  187. void
  188. png_write_PLTE(png_struct *png_ptr, png_color *palette, int number)
  189. {
  190.    int i;
  191.    png_color *pal_ptr;
  192.    png_byte buf[3];
  193.  
  194.    png_write_chunk_start(png_ptr, png_PLTE, number * 3);
  195.    for (i = 0, pal_ptr = palette;
  196.       i < number;
  197.       i++, pal_ptr++)
  198.    {
  199.       buf[0] = pal_ptr->red;
  200.       buf[1] = pal_ptr->green;
  201.       buf[2] = pal_ptr->blue;
  202.       png_write_chunk_data(png_ptr, buf, (png_uint_32)3);
  203.    }
  204.    png_write_chunk_end(png_ptr);
  205. }
  206.  
  207. /* write an IDAT chunk */
  208. void
  209. png_write_IDAT(png_struct *png_ptr, png_byte *data, png_uint_32 length)
  210. {
  211.    png_write_chunk(png_ptr, png_IDAT, data, length);
  212. }
  213.  
  214. /* write an IEND chunk */
  215. void
  216. png_write_IEND(png_struct *png_ptr)
  217. {
  218.    png_write_chunk(png_ptr, png_IEND, NULL, (png_uint_32)0);
  219. }
  220.  
  221. /* write a gAMA chunk */
  222. void
  223. png_write_gAMA(png_struct *png_ptr, float gamma)
  224. {
  225.    png_uint_32 igamma;
  226.    png_byte buf[4];
  227.  
  228.    /* gamma is saved in 1/100,000ths */
  229.    igamma = (png_uint_32)(gamma * 100000.0 + 0.5);
  230.    png_save_uint_32(buf, igamma);
  231.    png_write_chunk(png_ptr, png_gAMA, buf, (png_uint_32)4);
  232. }
  233.  
  234. /* write the sBIT chunk */
  235. void
  236. png_write_sBIT(png_struct *png_ptr, png_color_8 *sbit, int color_type)
  237. {
  238.    png_byte buf[4];
  239.    int size;
  240.  
  241.    /* make sure we don't depend upon the order of png_color_8 */
  242.    if (color_type & PNG_COLOR_MASK_COLOR)
  243.    {
  244.       buf[0] = sbit->red;
  245.       buf[1] = sbit->green;
  246.       buf[2] = sbit->blue;
  247.       size = 3;
  248.    }
  249.    else
  250.    {
  251.       buf[0] = sbit->gray;
  252.       size = 1;
  253.    }
  254.  
  255.    if (color_type & PNG_COLOR_MASK_ALPHA)
  256.    {
  257.       buf[size++] = sbit->alpha;
  258.    }
  259.  
  260.    png_write_chunk(png_ptr, png_sBIT, buf, (png_uint_32)size);
  261. }
  262.  
  263. /* write the cHRM chunk */
  264. void
  265. png_write_cHRM(png_struct *png_ptr, float white_x, float white_y,
  266.    float red_x, float red_y, float green_x, float green_y,
  267.    float blue_x, float blue_y)
  268. {
  269.    png_uint_32 itemp;
  270.    png_byte buf[32];
  271.  
  272.    /* each value is saved int 1/100,000ths */
  273.    itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
  274.    png_save_uint_32(buf, itemp);
  275.    itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
  276.    png_save_uint_32(buf, itemp);
  277.    itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
  278.    png_save_uint_32(buf, itemp);
  279.    itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
  280.    png_save_uint_32(buf, itemp);
  281.    itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
  282.    png_save_uint_32(buf, itemp);
  283.    itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
  284.    png_save_uint_32(buf, itemp);
  285.    itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
  286.    png_save_uint_32(buf, itemp);
  287.    itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
  288.    png_save_uint_32(buf, itemp);
  289.    png_write_chunk(png_ptr, png_cHRM, buf, (png_uint_32)32);
  290. }
  291.  
  292. /* write the tRNS chunk */
  293. void
  294. png_write_tRNS(png_struct *png_ptr, png_byte *trans, png_color_16 *tran,
  295.    int num_trans, int color_type)
  296. {
  297.    png_byte buf[6];
  298.  
  299.    if (color_type == PNG_COLOR_TYPE_PALETTE)
  300.    {
  301.       /* write the chunk out as it is */
  302.       png_write_chunk(png_ptr, png_tRNS, trans, (png_uint_32)num_trans);
  303.    }
  304.    else if (color_type == PNG_COLOR_TYPE_GRAY)
  305.    {
  306.       /* one 16 bit value */
  307.       png_save_uint_16(buf, tran->gray);
  308.       png_write_chunk(png_ptr, png_tRNS, buf, (png_uint_32)2);
  309.    }
  310.    else if (color_type == PNG_COLOR_TYPE_RGB)
  311.    {
  312.       /* three 16 bit values */
  313.       png_save_uint_16(buf, tran->red);
  314.       png_save_uint_16(buf + 2, tran->green);
  315.       png_save_uint_16(buf + 4, tran->blue);
  316.       png_write_chunk(png_ptr, png_tRNS, buf, (png_uint_32)6);
  317.    }
  318. }
  319.  
  320. /* write the background chunk */
  321. void
  322. png_write_bKGD(png_struct *png_ptr, png_color_16 *back, int color_type)
  323. {
  324.    png_byte buf[6];
  325.  
  326.    if (color_type == PNG_COLOR_TYPE_PALETTE)
  327.    {
  328.       buf[0] = back->index;
  329.       png_write_chunk(png_ptr, png_bKGD, buf, (png_uint_32)1);
  330.    }
  331.    else if (color_type & PNG_COLOR_MASK_COLOR)
  332.    {
  333.       png_save_uint_16(buf, back->red);
  334.       png_save_uint_16(buf + 2, back->green);
  335.       png_save_uint_16(buf + 4, back->blue);
  336.       png_write_chunk(png_ptr, png_bKGD, buf, (png_uint_32)6);
  337.    }
  338.    else
  339.    {
  340.       png_save_uint_16(buf, back->gray);
  341.       png_write_chunk(png_ptr, png_bKGD, buf, (png_uint_32)2);
  342.    }
  343. }
  344.  
  345. /* write the histogram */
  346. void
  347. png_write_hIST(png_struct *png_ptr, png_uint_16 *hist, int number)
  348. {
  349.    int i;
  350.    png_byte buf[3];
  351.  
  352.    png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(number * 2));
  353.    for (i = 0; i < number; i++)
  354.    {
  355.       png_save_uint_16(buf, hist[i]);
  356.       png_write_chunk_data(png_ptr, buf, (png_uint_32)2);
  357.    }
  358.    png_write_chunk_end(png_ptr);
  359. }
  360.  
  361. /* write a tEXt chunk */
  362. void
  363. png_write_tEXt(png_struct *png_ptr, char *key, char *text,
  364.    png_uint_32 text_len)
  365. {
  366.    int key_len;
  367.  
  368.    key_len = strlen(key);
  369.    /* make sure we count the 0 after the key */
  370.    png_write_chunk_start(png_ptr, png_tEXt,
  371.       (png_uint_32)(key_len + text_len + 1));
  372.    /* key has an 0 at the end.  How nice */
  373.    png_write_chunk_data(png_ptr, (png_byte *)key, (png_uint_32)(key_len + 1));
  374.    png_write_chunk_data(png_ptr, (png_byte *)text, (png_uint_32)text_len);
  375.    png_write_chunk_end(png_ptr);
  376. }
  377.  
  378. /* write a compressed chunk */
  379. void
  380. png_write_zTXt(png_struct *png_ptr, char *key, char *text,
  381.    png_uint_32 text_len, int compression)
  382. {
  383.    int key_len;
  384.    char buf[1];
  385.    int i, ret;
  386.    char **output_ptr = NULL; /* array of pointers to output */
  387.    int num_output_ptr = 0; /* number of output pointers used */
  388.    int max_output_ptr = 0; /* size of output_ptr */
  389.  
  390.    key_len = strlen(key);
  391.  
  392.    /* we can't write the chunk until we find out how much data we have,
  393.       which means we need to run the compresser first, and save the
  394.       output.  This shouldn't be a problem, as the vast majority of
  395.       comments should be reasonable, but we will set up an array of
  396.       malloced pointers to be sure. */
  397.  
  398.    /* set up the compression buffers */
  399.    png_ptr->zstream->avail_in = (uInt)text_len;
  400.    png_ptr->zstream->next_in = (Byte *)text;
  401.    png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  402.    png_ptr->zstream->next_out = (Byte *)png_ptr->zbuf;
  403.  
  404.    /* this is the same compression loop as in png_write_row() */
  405.    do
  406.    {
  407.       /* compress the data */
  408.       ret = deflate(png_ptr->zstream, Z_NO_FLUSH);
  409.       if (ret != Z_OK)
  410.       {
  411.          /* error */
  412.          if (png_ptr->zstream->msg)
  413.             png_error(png_ptr, png_ptr->zstream->msg);
  414.          else
  415.             png_error(png_ptr, "zlib error");
  416.       }
  417.       /* check to see if we need more room */
  418.       if (!png_ptr->zstream->avail_out && png_ptr->zstream->avail_in)
  419.       {
  420.          /* make sure the output array has room */
  421.          if (num_output_ptr >= max_output_ptr)
  422.          {
  423.             max_output_ptr = num_output_ptr + 4;
  424.             if (output_ptr)
  425.                output_ptr = png_realloc(png_ptr, output_ptr,
  426.                   max_output_ptr * sizeof (char *));
  427.             else
  428.                output_ptr = png_malloc(png_ptr,
  429.                   max_output_ptr * sizeof (char *));
  430.          }
  431.  
  432.          /* save the data */
  433.          output_ptr[num_output_ptr] = png_large_malloc(png_ptr,
  434.             png_ptr->zbuf_size);
  435.          memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
  436.             (png_size_t)png_ptr->zbuf_size);
  437.          num_output_ptr++;
  438.  
  439.          /* and reset the buffer */
  440.          png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  441.          png_ptr->zstream->next_out = png_ptr->zbuf;
  442.       }
  443.    /* continue until we don't have anymore to compress */
  444.    } while (png_ptr->zstream->avail_in);
  445.  
  446.    /* finish the compression */
  447.    do
  448.    {
  449.       /* tell zlib we are finished */
  450.       ret = deflate(png_ptr->zstream, Z_FINISH);
  451.       if (ret != Z_OK && ret != Z_STREAM_END)
  452.       {
  453.          /* we got an error */
  454.          if (png_ptr->zstream->msg)
  455.             png_error(png_ptr, png_ptr->zstream->msg);
  456.          else
  457.             png_error(png_ptr, "zlib error");
  458.       }
  459.  
  460.       /* check to see if we need more room */
  461.       if (!png_ptr->zstream->avail_out && ret == Z_OK)
  462.       {
  463.          /* check to make sure our output array has room */
  464.          if (num_output_ptr >= max_output_ptr)
  465.          {
  466.             max_output_ptr = num_output_ptr + 4;
  467.             if (output_ptr)
  468.                output_ptr = png_realloc(png_ptr, output_ptr,
  469.                   max_output_ptr * sizeof (char *));
  470.             else
  471.                output_ptr = png_malloc(png_ptr,
  472.                   max_output_ptr * sizeof (char *));
  473.          }
  474.  
  475.          /* save off the data */
  476.          output_ptr[num_output_ptr] = png_large_malloc(png_ptr,
  477.             png_ptr->zbuf_size);
  478.          memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
  479.             (png_size_t)png_ptr->zbuf_size);
  480.          num_output_ptr++;
  481.  
  482.          /* and reset the buffer pointers */
  483.          png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  484.          png_ptr->zstream->next_out = png_ptr->zbuf;
  485.       }
  486.    } while (ret != Z_STREAM_END);
  487.  
  488.    /* text length is number of buffers plus last buffer */
  489.    text_len = png_ptr->zbuf_size * num_output_ptr;
  490.    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
  491.       text_len += (png_uint_32)(png_ptr->zbuf_size -
  492.          png_ptr->zstream->avail_out);
  493.  
  494.    /* write start of chunk */
  495.    png_write_chunk_start(png_ptr, png_zTXt,
  496.       (png_uint_32)(key_len + text_len + 2));
  497.    /* write key */
  498.    png_write_chunk_data(png_ptr, (png_byte *)key, (png_uint_32)(key_len + 1));
  499.    buf[0] = compression;
  500.    /* write compression */
  501.    png_write_chunk_data(png_ptr, (png_byte *)buf, (png_uint_32)1);
  502.  
  503.    /* write saved output buffers, if any */
  504.    for (i = 0; i < num_output_ptr; i++)
  505.    {
  506.       png_write_chunk_data(png_ptr, (png_byte *)output_ptr[i], png_ptr->zbuf_size);
  507.       png_large_free(png_ptr, output_ptr[i]);
  508.    }
  509.    if (max_output_ptr)
  510.       png_free(png_ptr, output_ptr);
  511.    /* write anything left in zbuf */
  512.    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
  513.       png_write_chunk_data(png_ptr, png_ptr->zbuf,
  514.          png_ptr->zbuf_size - png_ptr->zstream->avail_out);
  515.    /* close the chunk */
  516.    png_write_chunk_end(png_ptr);
  517.  
  518.    /* reset zlib for another zTXt or the image data */
  519.    deflateReset(png_ptr->zstream);
  520. }
  521.  
  522. /* write the pHYs chunk */
  523. void
  524. png_write_pHYs(png_struct *png_ptr, png_uint_32 x_pixels_per_unit,
  525.    png_uint_32 y_pixels_per_unit,
  526.    int unit_type)
  527. {
  528.    png_byte buf[9];
  529.  
  530.    png_save_uint_32(buf, x_pixels_per_unit);
  531.    png_save_uint_32(buf + 4, y_pixels_per_unit);
  532.    buf[8] = unit_type;
  533.  
  534.    png_write_chunk(png_ptr, png_pHYs, buf, (png_uint_32)9);
  535. }
  536.  
  537. /* write the oFFs chunk */
  538. void
  539. png_write_oFFs(png_struct *png_ptr, png_uint_32 x_offset,
  540.    png_uint_32 y_offset,
  541.    int unit_type)
  542. {
  543.    png_byte buf[9];
  544.  
  545.    png_save_uint_32(buf, x_offset);
  546.    png_save_uint_32(buf + 4, y_offset);
  547.    buf[8] = unit_type;
  548.  
  549.    png_write_chunk(png_ptr, png_oFFs, buf, (png_uint_32)9);
  550. }
  551.  
  552. /* two time chunks are given.  This chunk assumes you have a gmtime()
  553.    function.  If you don't have that, use the other tIME function */
  554. void
  555. png_write_tIME(png_struct *png_ptr, png_time *mod_time)
  556. {
  557.    png_byte buf[7];
  558.  
  559.    png_save_uint_16(buf, mod_time->year);
  560.    buf[2] = mod_time->month;
  561.    buf[3] = mod_time->day;
  562.    buf[4] = mod_time->hour;
  563.    buf[5] = mod_time->minute;
  564.    buf[6] = mod_time->second;
  565.  
  566.    png_write_chunk(png_ptr, png_tIME, buf, (png_uint_32)7);
  567. }
  568.  
  569. /* initializes the row writing capability of pnglib */
  570. void
  571. png_write_start_row(png_struct *png_ptr)
  572. {
  573.    /* set up row buffer */
  574.    png_ptr->row_buf = (png_byte *)png_large_malloc(png_ptr,
  575.       (((png_uint_32)png_ptr->usr_channels *
  576.       (png_uint_32)png_ptr->usr_bit_depth *
  577.       png_ptr->width) >> 3) + 1);
  578.    /* set up filtering buffers, if filtering */
  579.    if (png_ptr->bit_depth >= 8 && png_ptr->color_type != 3)
  580.    {
  581.       png_ptr->prev_row = (png_byte *)png_large_malloc(png_ptr,
  582.          png_ptr->rowbytes + 1);
  583.       memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  584.       png_ptr->save_row = (png_byte *)png_large_malloc(png_ptr,
  585.          png_ptr->rowbytes + 1);
  586.       memset(png_ptr->save_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  587.    }
  588.  
  589.    /* if interlaced, we need to set up width and height of pass */
  590.    if (png_ptr->interlaced)
  591.    {
  592.       if (!(png_ptr->transformations & PNG_INTERLACE))
  593.       {
  594.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  595.             png_pass_ystart[0]) / png_pass_yinc[0];
  596.          png_ptr->usr_width = (png_ptr->width +
  597.             png_pass_inc[0] - 1 -
  598.             png_pass_start[0]) /
  599.             png_pass_inc[0];
  600.       }
  601.       else
  602.       {
  603.          png_ptr->num_rows = png_ptr->height;
  604.          png_ptr->usr_width = png_ptr->width;
  605.       }
  606.    }
  607.    else
  608.    {
  609.       png_ptr->num_rows = png_ptr->height;
  610.       png_ptr->usr_width = png_ptr->width;
  611.    }
  612. }
  613.  
  614. /* Internal use only.   Called when finished processing a row of data */
  615. void
  616. png_write_finish_row(png_struct *png_ptr)
  617. {
  618.    int ret;
  619.  
  620.    /* next row */
  621.    png_ptr->row_number++;
  622.    /* see if we are done */
  623.    if (png_ptr->row_number < png_ptr->num_rows)
  624.       return;
  625.  
  626.    /* if interlaced, go to next pass */
  627.    if (png_ptr->interlaced)
  628.    {
  629.       png_ptr->row_number = 0;
  630.       if (png_ptr->transformations & PNG_INTERLACE)
  631.       {
  632.          png_ptr->pass++;
  633.       }
  634.       else
  635.       {
  636.          /* loop until we find a non-zero width or height pass */
  637.          do
  638.          {
  639.             png_ptr->pass++;
  640.             if (png_ptr->pass >= 7)
  641.                break;
  642.             png_ptr->usr_width = (png_ptr->width +
  643.                png_pass_inc[png_ptr->pass] - 1 -
  644.                png_pass_start[png_ptr->pass]) /
  645.                png_pass_inc[png_ptr->pass];
  646.             png_ptr->num_rows = (png_ptr->height +
  647.                png_pass_yinc[png_ptr->pass] - 1 -
  648.                png_pass_ystart[png_ptr->pass]) /
  649.                png_pass_yinc[png_ptr->pass];
  650.          } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
  651.  
  652.       }
  653.  
  654.       /* reset filter row */
  655.       if (png_ptr->prev_row)
  656.          memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  657.       /* if we have more data to get, go get it */
  658.       if (png_ptr->pass < 7)
  659.          return;
  660.    }
  661.  
  662.    /* if we get here, we've just written the last row, so we need
  663.       to flush the compressor */
  664.    do
  665.    {
  666.       /* tell the compressor we are done */
  667.       ret = deflate(png_ptr->zstream, Z_FINISH);
  668.       /* check for an error */
  669.       if (ret != Z_OK && ret != Z_STREAM_END)
  670.       {
  671.          if (png_ptr->zstream->msg)
  672.             png_error(png_ptr, png_ptr->zstream->msg);
  673.          else
  674.             png_error(png_ptr, "zlib error");
  675.       }
  676.       /* check to see if we need more room */
  677.       if (!png_ptr->zstream->avail_out && ret == Z_OK)
  678.       {
  679.          png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  680.          png_ptr->zstream->next_out = png_ptr->zbuf;
  681.          png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  682.       }
  683.    } while (ret != Z_STREAM_END);
  684.  
  685.    /* write any extra space */
  686.    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
  687.    {
  688.       png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
  689.          png_ptr->zstream->avail_out);
  690.    }
  691.  
  692.     deflateReset(png_ptr->zstream);
  693. }
  694.  
  695. /* pick out the correct pixels for the interlace pass.
  696.  
  697.    The basic idea here is to go through the row with a source
  698.    pointer and a destination pointer (sp and dp), and copy the
  699.    correct pixels for the pass.  As the row gets compacted,
  700.    sp will always be >= dp, so we should never overwrite anything.
  701.    See the default: case for the easiest code to understand.
  702.    */
  703. void
  704. png_do_write_interlace(png_row_info *row_info, png_byte *row, int pass)
  705. {
  706.    /* we don't have to do anything on the last pass (6) */
  707.    if (row && row_info && pass < 6)
  708.    {
  709.       /* each pixel depth is handled seperately */
  710.       switch (row_info->pixel_depth)
  711.       {
  712.          case 1:
  713.          {
  714.             png_byte *sp;
  715.             png_byte *dp;
  716.             int shift;
  717.             int d;
  718.             int value;
  719.             png_uint_32 i;
  720.  
  721.             dp = row;
  722.             d = 0;
  723.             shift = 7;
  724.             for (i = png_pass_start[pass];
  725.                i < row_info->width;
  726.                i += png_pass_inc[pass])
  727.             {
  728.                sp = row + (png_size_t)(i >> 3);
  729.                value = (int)(*sp >> (7 - (i & 7))) & 0x1;
  730.                d |= (value << shift);
  731.  
  732.                if (shift == 0)
  733.                {
  734.                   shift = 7;
  735.                   *dp++ = d;
  736.                   d = 0;
  737.                }
  738.                else
  739.                   shift--;
  740.  
  741.             }
  742.             if (shift != 7)
  743.                *dp = d;
  744.             break;
  745.          }
  746.          case 2:
  747.          {
  748.             png_byte *sp;
  749.             png_byte *dp;
  750.             int shift;
  751.             int d;
  752.             int value;
  753.             png_uint_32 i;
  754.  
  755.             dp = row;
  756.             shift = 6;
  757.             d = 0;
  758.             for (i = png_pass_start[pass];
  759.                i < row_info->width;
  760.                i += png_pass_inc[pass])
  761.             {
  762.                sp = row + (i >> 2);
  763.                value = (*sp >> ((3 - (i & 3)) << 1)) & 0x3;
  764.                d |= (value << shift);
  765.  
  766.                if (shift == 0)
  767.                {
  768.                   shift = 6;
  769.                   *dp++ = d;
  770.                   d = 0;
  771.                }
  772.                else
  773.                   shift -= 2;
  774.             }
  775.             if (shift != 6)
  776.                    *dp = d;
  777.             break;
  778.          }
  779.          case 4:
  780.          {
  781.             png_byte *sp;
  782.             png_byte *dp;
  783.             int shift;
  784.             int d;
  785.             int value;
  786.             png_uint_32 i;
  787.  
  788.             dp = row;
  789.             shift = 4;
  790.             d = 0;
  791.             for (i = png_pass_start[pass];
  792.                i < row_info->width;
  793.                i += png_pass_inc[pass])
  794.             {
  795.                sp = row + (i >> 1);
  796.                value = (*sp >> ((1 - (i & 1)) << 2)) & 0xf;
  797.                d |= (value << shift);
  798.  
  799.                if (shift == 0)
  800.                {
  801.                   shift = 4;
  802.                   *dp++ = d;
  803.                   d = 0;
  804.                }
  805.                else
  806.                   shift -= 4;
  807.             }
  808.             if (shift != 4)
  809.                *dp = d;
  810.             break;
  811.          }
  812.          default:
  813.          {
  814.             png_byte *sp;
  815.             png_byte *dp;
  816.             png_uint_32 i;
  817.             int pixel_bytes;
  818.  
  819.             /* start at the beginning */
  820.             dp = row;
  821.             /* find out how many bytes each pixel takes up */
  822.             pixel_bytes = (row_info->pixel_depth >> 3);
  823.             /* loop through the row, only looking at the pixels that
  824.                matter */
  825.             for (i = png_pass_start[pass];
  826.                i < row_info->width;
  827.                i += png_pass_inc[pass])
  828.             {
  829.                /* find out where the original pixel is */
  830.                sp = row + i * pixel_bytes;
  831.                /* move the pixel */
  832.                if (dp != sp)
  833.                   memcpy(dp, sp, pixel_bytes);
  834.                /* next pixel */
  835.                dp += pixel_bytes;
  836.             }
  837.             break;
  838.          }
  839.       }
  840.       /* set new row width */
  841.       row_info->width = (row_info->width +
  842.          png_pass_inc[pass] - 1 -
  843.          png_pass_start[pass]) /
  844.          png_pass_inc[pass];
  845.       row_info->rowbytes = ((row_info->width *
  846.          row_info->pixel_depth + 7) >> 3);
  847.  
  848.    }
  849. }
  850.  
  851. /* this filters the row.  Both row and prev_row have space at the
  852.    first byte for the filter byte. */
  853. void
  854. png_write_filter_row(png_row_info *row_info, png_byte *row,
  855.    png_byte *prev_row)
  856. {
  857.    int minf, bpp;
  858.    png_uint_32 i, v;
  859.    png_uint_32 s, mins;
  860.    png_byte *rp, *pp, *cp, *lp;
  861.  
  862.    /* find out how many bytes offset each pixel is */
  863.    bpp = (row_info->pixel_depth + 7) / 8;
  864.    if (bpp < 1)
  865.       bpp = 1;
  866.  
  867.    /* the prediction method we use is to find which method provides
  868.       the smallest value when summing the abs of the distances from
  869.       zero using anything >= 128 as negitive numbers. */
  870.    for (i = 0, s = 0, rp = row + 1; i < row_info->rowbytes; i++, rp++)
  871.    {
  872.       v = *rp;
  873.       if (v < 128)
  874.          s += v;
  875.       else
  876.          s += 256 - (png_int_32)v;
  877.    }
  878.  
  879.    mins = s;
  880.    minf = 0;
  881.  
  882.    /* check sub filter */
  883.    for (i = 0, s = 0, rp = row + 1, lp = row + 1 - bpp;
  884.       i < row_info->rowbytes; i++, rp++, lp++)
  885.    {
  886.       if (i >= bpp)
  887.          v = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  888.       else
  889.          v = *rp;
  890.  
  891.       if (v < 128)
  892.          s += v;
  893.       else
  894.          s += 256 - v;
  895.    }
  896.  
  897.    if (s < mins)
  898.    {
  899.       mins = s;
  900.       minf = 1;
  901.    }
  902.  
  903.    /* check up filter */
  904.    for (i = 0, s = 0, rp = row + 1, pp = prev_row + 1;
  905.       i < row_info->rowbytes; i++, rp++, pp++)
  906.    {
  907.       v = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  908.  
  909.       if (v < 128)
  910.          s += v;
  911.       else
  912.          s += 256 - v;
  913.    }
  914.  
  915.    if (s < mins)
  916.    {
  917.       mins = s;
  918.       minf = 2;
  919.    }
  920.  
  921.    /* check avg filter */
  922.    for (i = 0, s = 0, rp = row + 1, pp = prev_row + 1, lp = row + 1 - bpp;
  923.       i < row_info->rowbytes; i++, rp++, pp++, lp++)
  924.    {
  925.       if (i >= bpp)
  926.          v = (png_byte)(((int)*rp - (((int)*pp + (int)*lp) / 2)) & 0xff);
  927.       else
  928.          v = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
  929.  
  930.       if (v < 128)
  931.          s += v;
  932.       else
  933.          s += 256 - v;
  934.    }
  935.  
  936.    if (s < mins)
  937.    {
  938.       mins = s;
  939.       minf = 3;
  940.    }
  941.  
  942.    /* check paeth filter */
  943.    for (i = 0, s = 0, rp = row + 1, pp = prev_row + 1, lp = row + 1 - bpp,
  944.          cp = prev_row + 1 - bpp;
  945.       i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
  946.    {
  947.       int a, b, c, pa, pb, pc, p;
  948.  
  949.       b = *pp;
  950.       if (i >= bpp)
  951.       {
  952.          c = *cp;
  953.          a = *lp;
  954.       }
  955.       else
  956.       {
  957.          a = c = 0;
  958.       }
  959.       p = a + b - c;
  960.       pa = abs(p - a);
  961.       pb = abs(p - b);
  962.       pc = abs(p - c);
  963.  
  964.       if (pa <= pb && pa <= pc)
  965.          p = a;
  966.       else if (pb <= pc)
  967.          p = b;
  968.       else
  969.          p = c;
  970.  
  971.       v = (png_byte)(((int)*rp - p) & 0xff);
  972.  
  973.       if (v < 128)
  974.          s += v;
  975.       else
  976.          s += 256 - v;
  977.    }
  978.  
  979.    if (s < mins)
  980.    {
  981.       mins = s;
  982.       minf = 4;
  983.    }
  984.  
  985.    /* set filter byte */
  986.    row[0] = minf;
  987.  
  988.    /* do filter */
  989.    switch (minf)
  990.    {
  991.       /* sub filter */
  992.       case 1:
  993.          for (i = bpp, rp = row + (png_size_t)row_info->rowbytes,
  994.             lp = row + (png_size_t)row_info->rowbytes - bpp;
  995.             i < row_info->rowbytes; i++, rp--, lp--)
  996.          {
  997.             *rp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  998.          }
  999.          break;
  1000.       /* up filter */
  1001.       case 2:
  1002.          for (i = 0, rp = row + (png_size_t)row_info->rowbytes,
  1003.             pp = prev_row + (png_size_t)row_info->rowbytes;
  1004.             i < row_info->rowbytes; i++, rp--, pp--)
  1005.          {
  1006.             *rp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  1007.          }
  1008.          break;
  1009.       /* avg filter */
  1010.       case 3:
  1011.          for (i = row_info->rowbytes,
  1012.             rp = row + (png_size_t)row_info->rowbytes,
  1013.             pp = prev_row + (png_size_t)row_info->rowbytes,
  1014.             lp = row + (png_size_t)row_info->rowbytes - bpp;
  1015.             i > bpp; i--, rp--, lp--, pp--)
  1016.          {
  1017.             *rp = (png_byte)(((int)*rp - (((int)*lp + (int)*pp) /
  1018.                2)) & 0xff);
  1019.          }
  1020.          for (; i > 0; i--, rp--, pp--)
  1021.          {
  1022.             *rp = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
  1023.          }
  1024.          break;
  1025.       /* paeth filter */
  1026.       case 4:
  1027.          for (i = row_info->rowbytes,
  1028.             rp = row + (png_size_t)row_info->rowbytes,
  1029.             pp = prev_row + (png_size_t)row_info->rowbytes,
  1030.             lp = row + (png_size_t)row_info->rowbytes - bpp,
  1031.             cp = prev_row + (png_size_t)row_info->rowbytes - bpp;
  1032.             i > 0; i--, rp--, lp--, pp--, cp--)
  1033.          {
  1034.             int a, b, c, pa, pb, pc, p;
  1035.  
  1036.             b = *pp;
  1037.             if (i > bpp)
  1038.             {
  1039.                c = *cp;
  1040.                a = *lp;
  1041.             }
  1042.             else
  1043.             {
  1044.                a = c = 0;
  1045.             }
  1046.             p = a + b - c;
  1047.             pa = abs(p - a);
  1048.             pb = abs(p - b);
  1049.             pc = abs(p - c);
  1050.  
  1051.             if (pa <= pb && pa <= pc)
  1052.                p = a;
  1053.             else if (pb <= pc)
  1054.                p = b;
  1055.             else
  1056.                p = c;
  1057.  
  1058.             *rp = (png_byte)(((int)*rp - p) & 0xff);
  1059.          }
  1060.          break;
  1061.    }
  1062. }
  1063.